<?php
namespace APP\C;

class Login extends Controller
{
    /**
     * 显示登陆
     */
	public function indexAction()
	{
		$this->view->render('login.tpl');
	}


    /**
     * 执行登陆
     */
    public function doAction()
    {
        $result = new \SF\Result();
        $form = new \APP\PUT\Login($this->request->getPost());

        if ($form->isValid()) {
            $sql = 'SELECT id, password FROM admin_user WHERE email="'. $form->email .'"';
            if (true == ($userInfo = $this->db->fetchRow($sql))) {
                if ($userInfo['password'] == md5($form->password)) {
                    \SF\Session\Session::set(self::SESS_ADMIN, $form->email);
                    $result->setValid();
                } else {
                    $result->setMessage('密码错误');
                }
            } else {
                $result->setMessage('管理用户不存在');
            }
        } else {
            $result->setMessage($form->getMessage());
        }

        echo $result->getByJson();
        exit;
    }

    
    /**
     * 退出登陆
     */
    public function logoutAction()
    {
        \SF\Session\Session::destroy(self::SESS_ADMIN);
        $this->router->redirect('/');
    }
}